package com.servlet;

import com.Imp.DeliverymanMapperImpl;
import com.Imp.UserMapperImpl;
import com.entity.Deliveryman;
import com.entity.Merchant;
import com.entity.User;
import com.mapper.DeliverymanMapper;
import com.mapper.UserMapper;

import javax.net.ssl.HandshakeCompletedEvent;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class DeliverymanController extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request,response);
    }

    public void  doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException{
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");

        HttpSession session = request.getSession();
        String userName = (String) session.getAttribute("username");
        //获取所有登录数据
        String name=(String)request.getParameter("name");
        String gender=(String)request.getParameter("gender");
        String age=(String)request.getParameter("age");
        String phoneNumber=(String)request.getParameter("phoneNumber");
        //根据用户账户获取到外卖员信息
        DeliverymanMapper deliverymanMapper = new DeliverymanMapperImpl();
        Deliveryman deliveryman = deliverymanMapper.getDeliverymanByUserName(userName);
        UserMapper userMapper = new UserMapperImpl();
        User user = userMapper.getUserByUsername(userName);

        if(deliveryman == null){
            deliveryman = new Deliveryman();
            deliveryman.setName(name);
            deliveryman.setUserName(userName);
            deliveryman.setSex(gender);
            deliveryman.setPassWord(user.getPassWord());
            deliveryman.setAge(age);
            deliveryman.setPhone(phoneNumber);
            //将更改后的信息插入数据库中
            deliverymanMapper.addDeliveryman(deliveryman);
            response.sendRedirect("information_deliveryman.jsp");
        }
        else{
            deliveryman.setName(name);
            deliveryman.setSex(gender);
            deliveryman.setAge(age);
            deliveryman.setPhone(phoneNumber);
            //将更改后的信息插入数据库中
            deliverymanMapper.updateDeliveryman(deliveryman);
            response.sendRedirect("information_deliveryman.jsp");
        }

    }
}
